package com.antoco.controlcenter.comm.codec;

import java.util.ArrayList;
import java.util.List;

public class DistanceByte2MessageDecoder {
    private final int pLength = 8; //协议定长长度
    private int recPtr = 0;
    private byte[] recBuffer;

    enum DecodeState {
        Idle,
        Receving,
    }

    DecodeState state;

    public DistanceByte2MessageDecoder() {
        recBuffer = new byte[pLength];
        state = DecodeState.Idle;
    }

    public Object decode(byte[] buffer) {
        List<Object> retList = new ArrayList<Object>();
        for(byte element : buffer)
        {
            switch (this.state) {
                case Idle:
                    if (element == 0x55) {
                        this.recPtr = 0;
                        this.recBuffer[this.recPtr++] = element;
                        this.state = DecodeState.Receving;
                    } else {
                        //LogHelper.WARNING("接收到错误数据：" + data.ToString("X2"));
                    }
                    break;
                case Receving:
                    this.recBuffer[this.recPtr++] = element;
                    if (this.recPtr > (pLength - 1)) {
                        this.state = DecodeState.Idle;
                        retList.add(this.recBuffer.clone());
                    }
                    break;
            }
        }

        return  retList;
    }
}
